package com.examples.oryarok;

import static com.examples.oryarok.Constants.FROM;
import static com.examples.oryarok.Constants.TABLE_NAME;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListList extends Activity {
	private MeetDataDbHelper meetEntry;
	//private TextView lineEntry;
	private LinearLayout line;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_list);
        line = (LinearLayout)findViewById(R.id.list_scroll);
        
        
    }
	
	@Override
	public void onResume() {
		super.onResume();
		meetEntry = new MeetDataDbHelper(this);
		try {
			Cursor cursor = getCursor();
			showEntry(cursor);
		}
		finally {
			meetEntry.close();
		}
		
	}

	private Cursor getCursor() {
		SQLiteDatabase db = meetEntry.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, null);
		startManagingCursor(cursor);
		return cursor;
	}
	
	private void showEntry(Cursor cursor) {
		//Stuff them all into a big string
		StringBuilder strEntry;
		boolean hasEntry = cursor.moveToFirst();
		if(hasEntry){
			while (hasEntry) {
				strEntry = new StringBuilder("");
				// Could use getColumnIndexOrThrow() to get indexes
				long id = cursor.getLong(0);
				String date = cursor.getString(1);
				//String day = cursor.getString(2);
				String time = cursor.getString(2);
				String name = cursor.getString(3);
				String phone = cursor.getString(5);
				strEntry.append(id).append(": ");
				strEntry.append(date).append(", ");
				//strEntry.append(day).append(", ");
				strEntry.append(time).append(" - ");
				strEntry.append(name).append(" ");
				strEntry.append(phone);
				
				
				// Display on the screen
				View lineThin = new View(this);
				TextView txtTemp = new TextView(this);
				lineThin.setBackgroundColor(getResources().getColor(R.color.line_gray));
				
				txtTemp.setText(strEntry);
				line.addView(txtTemp, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				line.addView(lineThin, LayoutParams.MATCH_PARENT, 1);
				hasEntry = cursor.moveToNext();
			}
		}
		else {
			TextView txtTemp = new TextView(this);
			txtTemp.setText("No entry in Data Base!");
			line.addView(txtTemp, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}
		

	}
	
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.list_list);
//        mListName = (EditText)findViewById(R.id.list_list_user);
//        mListDate = (EditText)findViewById(R.id.list_list_date);
//        mListAddButt= (Button)findViewById(R.id.but_list_list_add);
//        
//        mListAddButt.setOnClickListener(new View.OnClickListener() {
//			
//			public void onClick(View v) {
//				EntryAdder();
//				
//			}
//		});
//    }
//	
//	private void EntryAdder() {
//		String name = mListName.getText().toString();
//		String date = mListDate.getText().toString();
//		
//		meetEntry = new MeetData(this);
//        try {
//        	SQLiteDatabase db = meetEntry.getWritableDatabase();
//    		ContentValues values = new ContentValues();
//    		values.put(DATE, date);
//    		values.put(DAY, "Tue");
//    		values.put(TIME, "19:00");
//    		values.put(NAME, name);
//    		values.put(GENDER, "M");
//    		values.put(PHONE1, "0547590552");
//    		values.put(ADDRESS, "1151/25");
//    		values.put(STATUS, "ishur");
//    		values.put(TYPE, "miphgash");
//    		values.put(NUM_CONTACTS, 2);
//    		values.put(NOTE, "bla bla");
//    		db.insertOrThrow(TABLE_NAME, null, values);
//        	
//        }
//        finally {
//        	meetEntry.close();
//        }
//		
//	}

}